package cn.weiwei.WHadoop.hdfs.filesystem;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;

import java.net.URI;

/**
 * @author WangWeiwei
 * @version 1.0
 * @sine 17-2-6
 * 使用seek()方法，将Hadoop文件系统中的一个文件在标准输出上显示两次
 */
public class FileSystemDoubleCat {
    public void cat (String uri) throws Exception{
        Configuration conf = new Configuration();
        FileSystem fileSystem = FileSystem.get(URI.create(uri),conf);
        FSDataInputStream inputStream = null;
        try {
            inputStream = fileSystem.open(new Path(uri));
            IOUtils.copyBytes(inputStream,System.out,4096,false);
            inputStream.seek(0);//go back to the start of file
            IOUtils.copyBytes(inputStream,System.out,4096,false);
        }finally {
            IOUtils.closeStream(inputStream);
        }
    }
}
